//
//  SelectListViewController.m
//  CarLife
//
//  Created by yu on 2017/11/16.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "SelectListViewController.h"

#import "NearSelectView.h"

#import "NaviViewController.h"
#import "SpeechSynthesizer.h"

#import "NearSearchViewController.h"
#import "DriveManager.h"

@interface SelectListViewController ()<AMapNaviDriveManagerDelegate,DriveNaviDelegate>

@property (nonatomic, strong) NearSelectView *selectView;

@end

@implementation SelectListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [self.selectAnnotation title];
    [self setupParkingView];
    [self updateSubviewFrame];
    //[self initDriveManager];
    
    [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(40, 40, 170, 40) animated:NO];
    [self.mapView selectAnnotation:self.selectAnnotation animated:YES];
}

- (void)setupParkingView
{
    self.selectView = [[NSBundle mainBundle] loadNibNamed:@"NearSelectView" owner:self options:nil].firstObject;
    self.selectView.frame = CGRectMake(0, kScreenHeight - 64 - 130, kScreenWidth, 130);
    [self.view addSubview:self.selectView];
    
    @weakify(self);
    self.selectView.handleBlock = ^(NSInteger index) {
        @strongify(self);
        switch (index) {
            case 200:// 搜周边
                [self searchNearAction]; break;
            case 201:// 导航
                [self navigationAction]; break;
            default: break;
        }
    };
}

- (void)updateSubviewFrame
{
    self.userLocBtn.sd_layout.bottomSpaceToView(self.selectView, 10);
    self.zoomBtnContainer.sd_layout.bottomSpaceToView(self.selectView, 10);
}

- (void)searchNearAction
{
    NearSearchViewController *search = [[NearSearchViewController alloc] init];
    search.locationType = LocationByNearLocation;
    search.selectPoint = self.selectPoint;
    search.selectAnnotation = self.selectAnnotation;
    [self.navigationController pushViewController:search animated:YES];
    
//    PPLog(@"111 == %lf/%lf",self.selectPoint.latitude,self.selectPoint.longitude);
}

- (void)navigationAction
{
//    [self.driveManager calculateDriveRouteWithEndPoints:@[self.selectPoint]
//                                              wayPoints:nil drivingStrategy:AMapNaviDrivingStrategySingleDefault];
    [[AMapNaviDriveManager sharedInstance] setDelegate:self];
    [[AMapNaviDriveManager sharedInstance] calculateDriveRouteWithEndPoints:@[self.selectPoint]
    wayPoints:nil drivingStrategy:AMapNaviDrivingStrategySingleDefault];
    PPLog(@"777 = %lf/%lf",self.selectPoint.latitude,self.selectPoint.longitude);
}

//- (void)initDriveManager
//{
////    self.driveManager = [DriveManager sharedManager];
//    self.driveManager = [AMapNaviDriveManager sharedInstance];
//    [self.driveManager setDelegate:self];
//}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    SearchAnnotation *annotation = (SearchAnnotation *)view.annotation;
    self.selectPoint = [AMapNaviPoint locationWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
    self.navigationItem.title = annotation.title;
    [self.selectView updateInfoView:annotation];
}

#pragma mark - DriveNaviView Delegate

- (void)driveNaviViewCloseButtonClicked
{
    // 开始导航后不再允许选择路径，所以停止导航
    [[AMapNaviDriveManager sharedInstance] stopNavi];
    [[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - AMapNaviDriveManager Delegate

- (void)driveManager:(AMapNaviDriveManager *)driveManager error:(NSError *)error
{
    PPLog(@"error:{%ld - %@}", (long)error.code, error.localizedDescription);
}

//TODO:规划路线成功的回调
- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager
{
//    NaviViewController *driveVC = [[NaviViewController alloc] init];
//    [driveVC setDelegate:self];
//
//    //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
//    [self.driveManager addDataRepresentative:driveVC.driveView];
//
//    [self.navigationController pushViewController:driveVC animated:YES];
////    [self.driveManager setEmulatorNaviSpeed:120];
//    [self.driveManager startGPSNavi];
    
    NaviViewController *driveVC = [[NaviViewController alloc] init];
    [driveVC setDelegate:self];
    
    //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
    [[AMapNaviDriveManager sharedInstance] addDataRepresentative:driveVC.driveView];
    
    [self.navigationController pushViewController:driveVC animated:NO];
    //        [self.driveManager setEmulatorNaviSpeed:120];
    [[AMapNaviDriveManager sharedInstance] startGPSNavi];
    
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteFailure:(NSError *)error
{
    [MBProgressHUD showToast:error.description];
    PPLog(@"onCalculateRouteFailure:{%ld - %@}", (long)error.code, error.localizedDescription);
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager didStartNavi:(AMapNaviMode)naviMode
{
    PPLog(@"didStartNavi");
}

- (void)driveManagerNeedRecalculateRouteForYaw:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"needRecalculateRouteForYaw");
}

- (void)driveManagerNeedRecalculateRouteForTrafficJam:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"needRecalculateRouteForTrafficJam");
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager onArrivedWayPoint:(int)wayPointIndex
{
    PPLog(@"onArrivedWayPoint:%d", wayPointIndex);
}

- (BOOL)driveManagerIsNaviSoundPlaying:(AMapNaviDriveManager *)driveManager
{
    return [[SpeechSynthesizer sharedSpeechSynthesizer] isSpeaking];
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType
{
    PPLog(@"playNaviSoundString:{%ld:%@}", (long)soundStringType, soundString);
    [[SpeechSynthesizer sharedSpeechSynthesizer] speakString:soundString];
}

- (void)driveManagerDidEndEmulatorNavi:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"didEndEmulatorNavi");
}

- (void)driveManagerOnArrivedDestination:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"onArrivedDestination");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
